-- UIBuildingSubmit
-- Created by chengb Dec/31/2015
-- 建筑提交界面

UIBuildingSubmit = class("UIBuildingSubmit", function()
    return cc.Layer:create();
end);

function UIBuildingSubmit.create(gridData, okFunc, openType, bonus, extraPara)
    return UIBuildingSubmit.new(gridData, okFunc, openType, bonus, extraPara);
end

-- 构造函数
function UIBuildingSubmit:ctor(gridData, okFunc, openType, bonus, extraPara)
    self:setName("UIBuildingSubmit");

    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView5.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    trace("UIBuildingSubmit", "openType = %s", tostring(openType));

    self.gridData = gridData;
    self.okFunc   = okFunc;
    self.openType = openType;
    self.bonus    = bonus;
    self.extraPara = extraPara;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBuildingSubmit:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBuildingSubmit:registerEventCallback()
    -- 关注放入装备的事件
    EventMgr.register("UIBuildingSubmit", event.BLACKSMITH_INPUT_EQUIP, function(args)
        self:whenInputEquip(args);
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBuildingSubmit 界面析构清理");

            EventMgr.removeAll("UIBuildingSubmit");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBuildingSubmit:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingSubmit");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            if self.okFunc then
                self.okFunc(self.equipId);

                -- 关闭界面
                closeFormByName("UIBuildingSubmit");
            end
        end
    end
    AddTouchEventListener(btnOk, onOkClick);
    self.btnOk = btnOk;

    -- 注册装备点击格点击事件
    local equipGrid = findChildByName(node, "CT/equip_node/frame");
    local function onEquipClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 打开装备选择界面
            local uiEquip = UIEquips.create(self.openType, self.extraPara);
            UIMgr.getCurrentScene():addForm(uiEquip);
        end
    end
    equipGrid:addTouchEventListener(onEquipClick);
end

-- 重绘
function UIBuildingSubmit:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local btnOk      = findChildByName(self.node, "CT/btn_ok");

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);
    if not buildingInfo then
        error(string.format("获取不到(class=%d)的建筑配置", class));
    end

    -- 标题
    titleLabel:setString(buildingInfo["title"]);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 建筑图标
    iconImg:loadTexture(getLevelItemIconPath(buildingInfo["icon"]));
    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end
    iconImg:setPositionY(iconImg:getPositionY() + cfgOffsetY);
    iconImg:setScale(1.3);

    -- 确定按钮文本
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(buildingInfo["view_text"]);

    -- 描述
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    local dialog = BuildingBonusM.getBuildingDialog(self.gridData);
    applyString(descLabel, dialog);

    -- 显示装备框
    local equipImg = findChildByName(self.node, "CT/equip_node/icon");
    self.equipImg = equipImg;
    self:redrawEquip();
end

-- 重绘装备格
function UIBuildingSubmit:redrawEquip()
    local frame = findChildByName(self.node, "CT/equip_node/frame");

    if not self.equipId then
        self.equipImg:setVisible(false);
        playLearnSkillHintEffect(frame, 70, 70);

        -- 提交按钮不可点击
        setButtonEnabled(self.btnOk, false)

        return;
    end

    -- 提交按钮可点击
    setButtonEnabled(self.btnOk, true);

    frame:removeAllChildren();
    self.equipImg:setVisible(true);
    local icon = ItemM.query(self.equipId, "icon");
    self.equipImg:loadTexture(getItemIconPath(icon));
end

-- 重绘提交按钮
function UIBuildingSubmit:redrawBtnOk(bonus, equipId)
    -- 默认不置灰
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    if not bonus then
        btnOk:setBright(true);
        btnOk:setTouchEnabled(true);
        return;
    end

    -- 检查是否置灰
    for _, c in pairs(bonus) do
        if c == equipId then
            btnOk:setBright(true);
            btnOk:setTouchEnabled(true);
            return;
        end
    end

    btnOk:setBright(false);
    btnOk:setTouchEnabled(false);
end

-- 放入装备的回调
function UIBuildingSubmit:whenInputEquip(args)
    local equipId = args.classId;
    if not equipId then
        return;
    end

    -- 记录装备id
    self.equipId = equipId;

    -- 重绘装备格
    self:redrawEquip();

    -- 重绘提交按钮
    self:redrawBtnOk(self.bonus, self.equipId);
end







